package com.xiaoliu.tools;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashSet;
 
import org.springframework.stereotype.Component;

import com.xiaoliu.pojo.TableInfoPOJO;

@Component
public class TableInfo extends DBUtil {
	/**
	 * 通过表名获取到表的信息	
	 * @param tableName
	 */
	public  LinkedHashSet<TableInfoPOJO>getTableInfo(String tableName) {
		//获取连接
		Connection conn = getConnection();
		ResultSet rs=null;
		LinkedHashSet<TableInfoPOJO> set=new LinkedHashSet<>();
		try {
			//获取到表的元数据
			DatabaseMetaData meta=conn.getMetaData();
			//conn.getCatalog获取到数据库名字
			rs = meta.getColumns(conn.getCatalog(), null, tableName, null);
			while(rs.next()) {
					TableInfoPOJO pojo=new TableInfoPOJO();
					pojo.setColumnName(rs.getString("COLUMN_NAME"));
					pojo.setRemarks(rs.getString("REMARKS"));
					pojo.setTypeName(rs.getString("TYPE_NAME"));
					set.add(pojo);
			}
			return set;
		} catch (SQLException e) {
			log.error("DataBaseMetaData:{}","Error retrieving metadata");
			e.printStackTrace();
		}finally {
			if(null !=rs) {
				try {
					rs.close();
				} catch (SQLException e) {
					log.error("close ResultSet:{}","Close the abnormal");
					e.printStackTrace();
				}
			}
			//关闭连接
			closeConnection();
		}
		return null;
	}
}
